/*
 * Jakefile
 *
 * Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* enter the following information about you and your module */
var AUTHOR      = "Antoine Mercadal",
    EMAIL       = "antoine.mercadal@inframonde.eu",
    COMPANY     = "org.archipelproject",
    NAME        = "Archipel",
    SUMMARY     = "The next-gen virtualization orchestrator",
    VERSION     = "1.0";



require("./common.jake")

/* YOU SHOULD NOT HAVE TO CHANGE THE FOLLOWING */
var ENV = require("system").env,
    stream = require("narwhal/term").stream,
    FILE = require("file"),
    JAKE = require("jake"),
    task = require("jake").task,
    FileList = JAKE.FileList,
    app = require("cappuccino/jake").app,
    configuration = ENV["CONFIG"] ||  "Debug",
    OS = require("os");

$DOCUMENTATION_BUILD = FILE.join("Build", "Documentation");

app ("Archipel", function(task)
{
    ENV["OBJJ_INCLUDE_PATHS"] = "Frameworks/Debug:Frameworks";

    task.setBuildIntermediatesPath(FILE.join("Build", NAME + ".build", configuration));
    task.setBuildPath(FILE.join("Build", configuration));
    task.setPreventsNib2Cib(true);
    task.setProductName(NAME);
    task.setIdentifier( COMPANY + "." + NAME);
    task.setVersion(VERSION);
    task.setAuthor(AUTHOR);
    task.setEmail(EMAIL);
    task.setSummary(SUMMARY);
    task.setSources(new FileList("*.j", "Controllers/*.j", "Views/*.j", "Model/*.j", "Categories/*.j", "Utils/*.j"));
    task.setResources(new FileList("Resources/**/**"));
    task.setInfoPlistPath("Info.plist");

    if (configuration === "Debug")
        task.setCompilerFlags("-DDEBUG -g");
    else
        task.setCompilerFlags("-O");
});

task ("build", ["Archipel"], function()
{
    printResults(configuration);
});

task ("debug", function()
{
    ENV["CONFIG"] = "Debug";
    JAKE.subjake(["."], "build", ENV);
});

task ("release", function()
{
    ENV["CONFIG"] = "Release";
    JAKE.subjake(["."], "build", ENV);
});

task ("run", ["debug"], function()
{
    OS.system(["open", FILE.join("Build", "Debug", "Archipel", "index.html")]);
});

task ("run-release", ["release"], function()
{
    OS.system(["open", FILE.join("Build", "Release", "Archipel", "index.html")]);
});

task ("press", ["release"], function()
{
    FILE.mkdirs(FILE.join("Build", "Press", "Archipel"));
    OS.system(["press", "-f", FILE.join("Build", "Release", "Archipel"), FILE.join("Build", "Press", "Archipel")]);
});

task ("flatten", ["press"], function()
{
    FILE.mkdirs(FILE.join("Build", "Flatten", "Archipel"));
    OS.system(["flatten", "-f", "--verbose", "--split", "3", "-c", "closure-compiler", FILE.join("Build", "Press", "Archipel"), FILE.join("Build", "Flatten", "Archipel")]);
});

task ("desktop", ["release"], function()
{
    FILE.mkdirs(FILE.join("Build", "Desktop", "Archipel"));
    require("cappuccino/nativehost").buildNativeHost(FILE.join("Build", "Release", "Archipel"), FILE.join("Build", "Desktop", "Archipel", "Archipel.app"));
    printResults("Desktop")
});

task ("run-desktop", ["desktop"], function()
{
    OS.system([FILE.join("Build", "Desktop", "Archipel", "Archipel.app", "Contents", "MacOS", "NativeHost"), "-i"]);
});

task ("docs", ["documentation"])

task ("documentation", function()
{
    // try to find a doxygen executable in the PATH;
    var doxygen = executableExists("doxygen");

    // If the Doxygen application is installed on Mac OS X, use that
    if (!doxygen && executableExists("mdfind"))
    {
        var p = OS.popen(["mdfind", "kMDItemContentType == 'com.apple.application-bundle' && kMDItemCFBundleIdentifier == 'org.doxygen'"]);
        if (p.wait() === 0)
        {
            var doxygenApps = p.stdout.read().split("\n");
            if (doxygenApps[0])
                doxygen = FILE.join(doxygenApps[0], "Contents/Resources/doxygen");
        }
    }

    if (doxygen && FILE.exists(doxygen))
    {
        stream.print("\0green(Using " + doxygen + " for doxygen binary.\0)");

        var documentationDir = FILE.join("Doxygen");

        if (OS.system([FILE.join(documentationDir, "make_headers.sh")]))
            OS.exit(1); //rake abort if ($? != 0)

        if (!OS.system([doxygen, FILE.join(documentationDir, "Archipel.doxygen")]))
        {
            rm_rf($DOCUMENTATION_BUILD);
            // mv("debug.txt", FILE.join("Documentation", "debug.txt"));
            mv("Documentation", $DOCUMENTATION_BUILD);
            cp(FILE.join(documentationDir, "doxygen.css"), FILE.join($DOCUMENTATION_BUILD, "html", "doxygen.css"));
        }

        OS.system(["ruby", FILE.join(documentationDir, "cleanup_headers")]);
    }
    else
        stream.print("\0yellow(Doxygen not installed, skipping documentation generation.\0)");
});

function printResults(configuration)
{
    print("----------------------------");
    print(configuration+" app built at path: "+FILE.join("Build", configuration, "Archipel"));
    print("----------------------------");
}
